package com.btex.app.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.btex.app.entity.BannerInfo;
import com.btex.app.tools.OptiosTool;
import com.btex.app.tools.ToUI;

import java.util.ArrayList;
import java.util.List;

public class MyPagerAdapter extends PagerAdapter {
    List<ImageView> imageViews = new ArrayList<ImageView>();
    ImageView iv;
    List<BannerInfo> bannerInfos;
    private Context context;
    private DisplayImageOptions options_normal;

    public MyPagerAdapter(List<BannerInfo> bannerInfos, Context context) {
        options_normal = OptiosTool
                .getOptions_normal();
        if (bannerInfos != null) {
            if (bannerInfos.size() == 2) {
                int j = 1;
                while (j <= 2) {
                    for (int i = 0; i < bannerInfos.size(); i++) {
                        iv = new ImageView(context);
                        LayoutParams lp = new LayoutParams(
                                LayoutParams.MATCH_PARENT,
                                LayoutParams.MATCH_PARENT);
                        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        ImageLoader.getInstance()
                                .displayImage(bannerInfos.get(i).getImg(), iv,
                                        options_normal);
                        iv.setLayoutParams(lp);
                        imageViews.add(iv);
                    }
                    j++;
                }

            } else {

                for (int i = 0; i < bannerInfos.size(); i++) {
                    iv = new ImageView(context);
                    LayoutParams lp = new LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.MATCH_PARENT);
                    iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    ImageLoader.getInstance().displayImage(
                            bannerInfos.get(i).getImg(), iv, options_normal);
                    iv.setLayoutParams(lp);
                    imageViews.add(iv);
                }
            }
            this.context = context;
            this.bannerInfos = bannerInfos;
        }
    }

    @Override
    public int getCount() {
        if(imageViews.size()==1)
            return 1;
        return imageViews.size() == 0 ? 0 : Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
//        container.removeView(imageViews.get(position % imageViews.size()));
    }

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {

        int newPosition = position % imageViews.size();
        Log.v("MyPagerAdapter", "获取第" + (position % imageViews.size()) + "张图片"+ " Imageview:" + iv);
        ImageView iv = imageViews.get(newPosition);

        try {
            container.addView(iv);
        } catch (Exception e) {
            e.printStackTrace();
        }

        iv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String url = "";
                if(bannerInfos.size()==2){
                    url = bannerInfos.get(position%2).getLink_url();
                }else{
                    url = bannerInfos.get(position % imageViews.size())
                            .getLink_url();
                }
                Intent i;

                i =new ToUI().toUi(context,url+"&window=new");
                if(i!=null) {
                    context.startActivity(i);
                    return;
                }
            }
        });
        return iv;
    }
}
